/* ****************************************************************************
 *
 * Copyright (c) Microsoft Corporation. 
 *
 * This source code is subject to terms and conditions of the Microsoft Public License. A 
 * copy of the license can be found in the License.html file at the root of this distribution. If 
 * you cannot locate the  Microsoft Public License, please send an email to 
 * dlr@microsoft.com. By using this source code in any fashion, you are agreeing to be bound 
 * by the terms of the Microsoft Public License.
 *
 * You must not remove this notice, or any other, from this software.
 *
 * THIS CODE AND INFORMATION ARE PROVIDED "AS IS" WITHOUT WARRANTY OF ANY
 * KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND/OR FITNESS FOR A
 * PARTICULAR PURPOSE.
 * ***************************************************************************/


#include "stdafx.h"
#include <pgl/core/PGLLinePropPage.h>
#include <pgl/core/PGLRegion.h>

#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif

/////////////////////////////////////////////////////////////////////////////
// CPGLLinePropPage property page

IMPLEMENT_DYNCREATE(CPGLLinePropPage, CPropertyPage)

CPGLLinePropPage::CPGLLinePropPage(CPGLLineStPtr _pLine) : CPropertyPage(CPGLLinePropPage::IDD)
{
	m_pLine=_pLine;
	//{{AFX_DATA_INIT(CPGLLinePropPage)
	m_lineWidth = 0.0f;
	m_pointWidth = 0.0f;
	m_pointType = -1;
	m_iInterpolationType = -1;
	m_bFilled = FALSE;
	m_bOpen = FALSE;
	m_bContour = FALSE;
	m_bSnapBottom = FALSE;
	m_lineType = -1;
	m_sCustomDashPattern = _T("");
	m_bShadow = FALSE;
	m_iStripSize = 0;
	m_dTension = 0.0;
	//}}AFX_DATA_INIT
}

CPGLLinePropPage::~CPGLLinePropPage()
{
}

void CPGLLinePropPage::DoDataExchange(CDataExchange* pDX)
{
	CPropertyPage::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(CPGLLinePropPage)
	DDX_Text(pDX, IDC_PGL_EDIT_LINE_WIDTH, m_lineWidth);
	DDV_MinMaxFloat(pDX, m_lineWidth, 1.e-002f, 100.f);
	DDX_Text(pDX, IDC_PGL_EDIT_POINT_WIDTH, m_pointWidth);
	DDV_MinMaxFloat(pDX, m_pointWidth, 0.f, 100.f);
	DDX_Radio(pDX, IDC_PGL_RADIO_POINT_NONE, m_pointType);
	DDX_Radio(pDX, IDC_PGL_RADIO_INTERPOLATION_STEP, m_iInterpolationType);
	DDX_Check(pDX, IDC_PGL_CHECK_FILL, m_bFilled);
	DDX_Check(pDX, IDC_PGL_LINE_OPEN_CHECK, m_bOpen);
	DDX_Check(pDX, IDC_PGL_CHECK_CONTOUR, m_bContour);
	DDX_Check(pDX, IDC_PGL_CHECK_SNAPBOTTOM, m_bSnapBottom);
	DDX_Radio(pDX, IDC_PGL_RADIO_LINE_CONT, m_lineType);
	DDX_Text(pDX, IDC_PGL_CUSTOM_DASH_PATTERN, m_sCustomDashPattern);
	DDX_Check(pDX, IDC_PGL_CHECK_SHADOW, m_bShadow);
	DDX_Text(pDX, IDC_PGL_EDIT_STRIP_SIZE, m_iStripSize);
	DDX_Text(pDX, IDC_PGL_EDIT_TENSION, m_dTension);
	DDV_MinMaxDouble(pDX, m_dTension, 0., 1.);
	//}}AFX_DATA_MAP
}


BEGIN_MESSAGE_MAP(CPGLLinePropPage, CPropertyPage)
	//{{AFX_MSG_MAP(CPGLLinePropPage)
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()


BOOL CPGLLinePropPage::OnKillActive() 
{
	UpdateData(TRUE);

	m_pLine->SetContoured(m_bContour);
	m_pLine->SetFilled(m_bFilled);
	m_pLine->SetOpen(m_bOpen);
	m_pLine->SetSnapBottom(m_bSnapBottom);
	m_pLine->SetShadowing(m_bShadow);

	m_pLine->SetStripSize(m_iStripSize);

	// radio buttons
	switch(m_iInterpolationType)
	{
	case 0: 
		m_pLine->SetInterpolationType(CPGLLine::InterpolationTypeStep); 
		break;
	case 1: 
		m_pLine->SetInterpolationType(CPGLLine::InterpolationTypeLinear); 
		break;
	case 2:
		m_pLine->SetInterpolationType(CPGLLine::InterpolationTypeSpline); 
		break;
	case 3:
		m_pLine->SetInterpolationType(CPGLLine::InterpolationTypeSegment); 
		break;
	}

	m_pLine->SetTension(m_dTension);

	switch(m_lineType)
	{
	case 0:
		m_pLine->SetDashPattern(PGL_LINE_CONT); 
		break;
	case 1:
		m_pLine->SetDashPattern(PGL_LINE_BARBAR); 
		break;
	case 2:
		m_pLine->SetDashPattern(PGL_LINE_BARPOINT); 
		break;
	case 3:
		m_pLine->SetDashPattern(PGL_LINE_POINTPOINT); 
		break;
	case 4:
		m_pLine->SetDashPattern(m_sCustomDashPattern);
	}

	switch(m_pointType)
	{
	case 0:
		m_pLine->SetPointType(CPGLLine::MarkerTypeNone);
		break;
	case 1:
		m_pLine->SetPointType(CPGLLine::MarkerTypeCircle);
		break;
	case 2:
		m_pLine->SetPointType(CPGLLine::MarkerTypeContour);
		break;
	case 3:
		m_pLine->SetPointType(CPGLLine::MarkerTypeTriangle);
		break;
	case 4:
		m_pLine->SetPointType(CPGLLine::MarkerTypeSquare);
		break;
	}

	m_pLine->SetPointWidth(m_pointWidth);
	m_pLine->SetLineWidth(m_lineWidth);
	
	return CPropertyPage::OnKillActive();
}

BOOL CPGLLinePropPage::OnSetActive() 
{
	PGL_ASSERT_VALID(m_pLine);

	m_bContour = m_pLine->IsContoured();
	m_bFilled = m_pLine->IsFilled();
	m_bOpen = m_pLine->IsOpen();
	m_bSnapBottom = m_pLine->IsSnapBottom();
	m_bShadow = m_pLine->IsShadowing();

	m_lineWidth=m_pLine->GetLineWidth();
	m_pointWidth=m_pLine->GetPointWidth();
	m_iStripSize = m_pLine->GetStripSize();


	// radio buttons
	switch(m_pLine->GetInterpolationType())
	{
	case CPGLLine::InterpolationTypeStep: 
		m_iInterpolationType=0; 
		break;
	case CPGLLine::InterpolationTypeLinear: 
		m_iInterpolationType=1; 
		break;
	case CPGLLine::InterpolationTypeSpline:
		m_iInterpolationType=2; 
		break;
	case CPGLLine::InterpolationTypeSegment:
		m_iInterpolationType=3; 
		break;
	}

	m_dTension = m_pLine->GetTension();
	
	m_sCustomDashPattern=_T("");
	if(m_pLine->GetDashPattern() == PGL_LINE_CONT)
		m_lineType=0; 
	else if (m_pLine->GetDashPattern() == PGL_LINE_BARBAR)
		m_lineType=1;
	else if (m_pLine->GetDashPattern() == PGL_LINE_BARPOINT)
		m_lineType=2;
	else if (m_pLine->GetDashPattern() == PGL_LINE_POINTPOINT)
		m_lineType=3;
	else
	{
		m_lineType=4;
		m_sCustomDashPattern = m_pLine->GetDashPattern();
	}

	UpdateData(FALSE);
	
	return CPropertyPage::OnSetActive();
}
